﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;
using BUS;

namespace giaodien
{
    public partial class QLNV : Form
    {
        public QLNV()
        {
            InitializeComponent();
        }
        
        int key = -1;
        private void splitContainer1_Panel1_MouseUp(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseUp();
        }

        private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseMove(this, MousePosition.X, MousePosition.Y);
        }

        private void splitContainer1_Panel1_MouseDown(object sender, MouseEventArgs e)
        {
            dichchuyen.MouseDown(e);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        public string taomatudong(string automanv)
        {
            string  mamoi;
            string laymaloannhathientai = "";
            int i = 0;
            DataSet ds = new DataSet();
            ds = nhanvien_BUS.automa();
            
            if (ds.Tables[0].Rows.Count != 0)
            {
                laymaloannhathientai = ds.Tables[0].Rows[0][0].ToString();
                mamoi = laymaloannhathientai.Substring(0, 3).ToString();
            }
            else
            {
                mamoi = "00";
            }
            i = int.Parse(mamoi);
            i++;
            return travegiatrima(i,automanv);
        }
        public string travegiatrima(int i, string st)
        {
            st = "";
            if (i < 10)
                return st.ToString() + "00" + i.ToString();
            else
                return st.ToString()  + "0" + i.ToString();
            
        }
        private void button6_Click(object sender, EventArgs e)
        {
            GDChinh frm = new GDChinh();
            DialogResult d = MessageBox.Show("Bạn Có Muốn Thoát", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (d == DialogResult.Yes)
            {
                this.Close();
                frm.Show();
            }
        }

        private void QLNV_Load(object sender, EventArgs e)
        {
            combogioitinh.Enabled = false;
            datengaysinh.Enabled = false;
            comboloainv.Enabled = false;
            DataSet ds = new DataSet();
            ds = nhanvien_BUS.loadthongtinnhanvienvaolistview();
            hienthithongtinnvlist(ds, listdsnv);            
        }
        void hienthithongtinnvlist(DataSet ds, ListView danhsach)
        {           
           danhsach.Items.Clear();
           for (int i=0; i<ds.Tables[0].Rows.Count;i++)
           {
               ListViewItem lvw = new ListViewItem();
               lvw.Text = ds.Tables[0].Rows[i][0].ToString();
               for (int j = 1; j < ds.Tables[0].Columns.Count; j++)
               {
                   if (ds.Tables[0].Rows[i][j].ToString() != "")
                   {
                       if (j == 6)

                           lvw.SubItems.Add(string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(ds.Tables[0].Rows[i][6].ToString()).ToShortDateString()));
                       else
                           lvw.SubItems.Add(ds.Tables[0].Rows[i][j].ToString());
                   }
                   else
                       lvw.SubItems.Add("");
               }
               danhsach.Items.Add(lvw);
           }
        }
           void laysukienkhimousedown(ListView danhsachmousedown, object sender, MouseEventArgs e)
           {
               if (e.Button != MouseButtons.Right)
               {
                   ListViewItem lvw = listdsnv.GetItemAt(e.X, e.Y);
                   nutthemnv.Enabled = true;
                   if (lvw != null)
                   {
                       txtmanv.Text = lvw.SubItems[0].Text;
                       txttenv.Text = lvw.SubItems[1].Text;
                       txtdiachi.Text = lvw.SubItems[2].Text;
                       combogioitinh.Text = lvw.SubItems[3].Text;
                       txtsdt.Text = lvw.SubItems[4].Text;
                       comboloainv.Text = lvw.SubItems[5].Text;
                       datengaysinh.Text = lvw.SubItems[6].Text;
                       txtcmnd.Text = lvw.SubItems[7].Text;
                       txttendangnhap.Text = lvw.SubItems[8].Text;
                       txtmk.Text = lvw.SubItems[9].Text;
                   }
               }
           }

           private void listdsnv_MouseDown(object sender, MouseEventArgs e)
           {
               laysukienkhimousedown(listdsnv, sender, e);
           }
           
           private void button1_Click(object sender, EventArgs e)
           {
               key = 0;
               txtmanv.Text = "";
               txttenv.Text = "";
               combogioitinh.Text = "";
               txtsdt.Text = "";
               txtcmnd.Text = "";
               datengaysinh.Text = "";
               comboloainv.Text = "";
               txtdiachi.Text = "";
               txttendangnhap.Text = "";
               txtmk.Text = "";
               txttendangnhap.ReadOnly = false;
               txtmk.ReadOnly = false;
               txttenv.ReadOnly = false;
               txtdiachi.ReadOnly = false;
               txtsdt.ReadOnly = false;
               txtcmnd.ReadOnly = false;
               combogioitinh.Enabled = true;
               comboloainv.Enabled = true;
               datengaysinh.Enabled = true;
               nutsuanv.Enabled = false;
           }

           private void nutthoat_Click(object sender, EventArgs e)
           {
               GDChinh frm = new GDChinh();
               DialogResult d = MessageBox.Show("Bạn có muốn thoát", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
               if (d == DialogResult.Yes)
               {
                   this.Close();
                   frm.Show();
               }
           }

           private void nutluunv_Click(object sender, EventArgs e)
           {
               if (key == 0)
               {
                   if ((txttenv.Text == "") || (txtsdt.Text == "") || (txtcmnd.Text == "") || (comboloainv.Text == "") || (combogioitinh.Text == "") || (txtdiachi.Text == "")||(txttendangnhap.Text=="")||(txtmk.Text==""))
                   {
                       MessageBox.Show("Vui lòng nhập đầy đủ thông tin");
                   }
                   else
                   {
                       DataSet ds = new DataSet();
                       nhanvien_DTO nv = new nhanvien_DTO();
                       nv.manv = "";
                       nv.manv = taomatudong(nv.manv);
                       nv.tennv = txttenv.Text;
                       nv.diachi = txtdiachi.Text;
                       nv.gioitinhnv = combogioitinh.Text;
                       nv.sdtnv = txtsdt.Text;
                       nv.loainv = comboloainv.Text;
                       nv.ngaysinh = DateTime.Parse(datengaysinh.Text);
                       nv.cmnd = int.Parse(txtcmnd.Text);
                       nv.tendangnhap = txttendangnhap.Text;
                       nv.mk = txtmk.Text;
                       if (nhanvien_BUS.themnv(nv) == true)
                       {
                           MessageBox.Show("Thêm thành công");
                           ds = nhanvien_BUS.loadthongtinnhanvienvaolistview();
                           hienthithongtinnvlist(ds, listdsnv);
                           txttenv.ReadOnly = true;
                           txtdiachi.ReadOnly = true;
                           txtsdt.ReadOnly = true;
                           txtcmnd.ReadOnly = true;
                           combogioitinh.Enabled = false;
                           comboloainv.Enabled = false;
                           datengaysinh.Enabled = false;
                           nutsuanv.Enabled = true;
                           txttendangnhap.ReadOnly = true;
                           txtmk.ReadOnly = true;
                           txttendangnhap.Text = "";
                           txtmk.Text = "";
                           txttenv.Text = "";
                           combogioitinh.Text = "";
                           txtsdt.Text = "";
                           txtcmnd.Text = "";
                           datengaysinh.Text = "";
                           comboloainv.Text = "";
                           txtdiachi.Text = "";
                       }
                       else
                           MessageBox.Show("Thêm không thành công");
                   }
               }
               if (key == 1)
               {
                   if ((txttenv.Text == "") || (txtsdt.Text == "") || (txtcmnd.Text == "") || (comboloainv.Text == "") || (combogioitinh.Text == "") || (txtdiachi.Text == "")||(txttendangnhap.Text=="")||(txtmk.Text==""))
                   {
                       MessageBox.Show("Vui lòng nhập đầy đủ thông tin");
                   }
                   else
                   {

                       foreach (ListViewItem lvw in listdsnv.SelectedItems)
                       {
                           DataSet ds = new DataSet();
                           nhanvien_DTO nv = new nhanvien_DTO();
                           nv.manv = lvw.Text;
                           nv.tennv = txttenv.Text;
                           nv.diachi = txtdiachi.Text;
                           nv.gioitinhnv = combogioitinh.Text;
                           nv.sdtnv = txtsdt.Text;
                           nv.loainv = comboloainv.Text;
                           nv.ngaysinh = DateTime.Parse(datengaysinh.Text);
                           nv.cmnd = Convert.ToInt32(txtcmnd.Text);
                           nv.tendangnhap = txttendangnhap.Text;
                           nv.mk = txtmk.Text;
                           DialogResult d = MessageBox.Show("Bạn có muốn sửa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                           if (d == DialogResult.Yes)
                           {
                               if (nhanvien_BUS.suanv(nv) == true)
                               {
                                   MessageBox.Show("Sửa thành công");
                                   ds = nhanvien_BUS.loadthongtinnhanvienvaolistview();
                                   hienthithongtinnvlist(ds, listdsnv);
                                   txttenv.ReadOnly = true;
                                   txtdiachi.ReadOnly = true;
                                   txtsdt.ReadOnly = true;
                                   txtcmnd.ReadOnly = true;
                                   txttendangnhap.ReadOnly = true;
                                   txtmk.ReadOnly = true;
                                   combogioitinh.Enabled = false;
                                   comboloainv.Enabled = false;
                                   datengaysinh.Enabled = false;
                               }
                               else
                                   MessageBox.Show("Sửa không thành công");
                           }
                       }
                   }
               }                            
           }
           private void nutxoanv_Click(object sender, EventArgs e)
           {
               DataSet ds = new DataSet();
               foreach (ListViewItem lvw in listdsnv.SelectedItems)
               {
                   if (lvw != null)
                   {
                       nhanvien_DTO nv = new nhanvien_DTO();
                       nv.manv = lvw.Text;

                       DialogResult d = MessageBox.Show("Bạn có chắc muốn xóa", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                       if (d == DialogResult.Yes)
                       {
                           if (nhanvien_BUS.xoanv(nv) == true)
                           {
                               MessageBox.Show("Xóa thành công");
                               ds = nhanvien_BUS.loadthongtinnhanvienvaolistview();
                               hienthithongtinnvlist(ds, listdsnv);
                               txtmanv.Text = "";
                               txttenv.Text = "";
                               combogioitinh.Text = "";
                               txtsdt.Text = "";
                               txtcmnd.Text = "";
                               datengaysinh.Text = "";
                               comboloainv.Text = "";
                               txtdiachi.Text = "";
                               txttendangnhap.Text = "";
                               txtmk.Text = "";
                           }
                           else
                           {
                               MessageBox.Show("Xóa không thành công");
                           }
                       }
                   }
               }
           }

           private void nutsuanv_Click(object sender, EventArgs e)
           {
               key = 1;
               txttendangnhap.ReadOnly = false;
               txtmk.ReadOnly = false;
               txttenv.ReadOnly = false;
               txtdiachi.ReadOnly = false;
               txtsdt.ReadOnly = false;
               txtcmnd.ReadOnly = false;
               combogioitinh.Enabled = true;
               comboloainv.Enabled = true;
               datengaysinh.Enabled = true;
           }

           private void nuttimnv_Click(object sender, EventArgs e)
           {
               nhanvien_DTO tennv = new nhanvien_DTO();
               DataSet ds = new DataSet();
               tennv.tennv = txttimkiemnv.Text;
               ds = nhanvien_BUS.timkiemnhanvien(tennv);
               hienthithongtinnvlist(ds, listdsnv);
           }

           private void txtsdt_KeyPress(object sender, KeyPressEventArgs e)
           {
               if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
               {
                   e.Handled = true;
               }
           }

           private void txtcmnd_KeyPress(object sender, KeyPressEventArgs e)
           {
               if (char.IsDigit(e.KeyChar) == false && char.IsControl(e.KeyChar) == false)
               {
                   e.Handled = true;
               }
           }

           private void combogioitinh_KeyPress(object sender, KeyPressEventArgs e)
           {
               e.Handled = true;
           }

           private void comboloainv_KeyPress(object sender, KeyPressEventArgs e)
           {
               e.Handled = true;
           }

           private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
           {

           }
    }
}
